// GTY


#include "Player/AuraPlayerState.h"
#include "AbilitySystem/AuraAbilitySystemComponent.h"
#include "AbilitySystem/AuraAttributeSet.h"
#include "Net/UnrealNetwork.h"

/*
 * 初始化  AbilitySystemComponent

ASC需要有OwnerActor和AvatarActor进行初始化，而且必须在服务器和客户端都要完成初始化。

对于玩家控制的角色，ASC存在于Pawn中，我通常在Pawn的 PossessedBy()方法中完成ASC在服务器端的初始化，
在PlayerController的AcknowledgePawn()方法中完成ASC在客户端的初始化。

对于玩家控制的角色，ASC存在于PlayerState中，
我通常在Pawn 的PossessedBy() 方法中完成ASC在服务器端的初始化（这一点与上述相同），
在 Pawn的 OnRep_PlayerState()方法中完成ASC在客户端的初始化（这将确保PlayerState在客户端已存在）。初始化AI角色的GAS

对于AI控制的角色，ASC存在于Pawn中，通常在Pawn的 BeginPlay()方法中完成ASC在服务器端和客户端的初始化。

既然根据文档知道了如何初始化，那么，我们按照相应的方式初始化。
首先初始化敌人身上的ASC，敌人作为ai怪物，只需要在BeginPlay里面初始化即可。
 */
AAuraPlayerState::AAuraPlayerState()
{
	AbilitySystemComponent = CreateDefaultSubobject<UAuraAbilitySystemComponent>("AbilitySystemComponent");
	//创建默认的AbilitySystemComponent
	AbilitySystemComponent->SetIsReplicated(true);
	//true，使组件状态在网络游戏中同步到所有客户端。
	AbilitySystemComponent->SetReplicationMode(EGameplayEffectReplicationMode::Mixed);
	//混合模式，在客户端和服务端都使用，但客户端更频繁地更新。
	
	
	AttributeSet = CreateDefaultSubobject<UAuraAttributeSet>("AttributeSet");
	//创建默认的AttributeSet

	NetUpdateFrequency = 100.f;//网络更新的频率，值越大，更新越频繁。
}

void AAuraPlayerState::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
	Super::GetLifetimeReplicatedProps(OutLifetimeProps);

	DOREPLIFETIME(AAuraPlayerState,Level);
	DOREPLIFETIME(AAuraPlayerState,XP);
	DOREPLIFETIME(AAuraPlayerState,Money);
	DOREPLIFETIME(AAuraPlayerState, AttributePoints);
	DOREPLIFETIME(AAuraPlayerState, SpellPoints);
}

UAbilitySystemComponent* AAuraPlayerState::GetAbilitySystemComponent() const
{
	return AbilitySystemComponent;
}

void AAuraPlayerState::AddToLevel(int32 InLevel)
{
	Level += InLevel;
	OnLevelChangedDelegate.Broadcast(Level);
}

void AAuraPlayerState::SetLevel(int32 InLevel)
{
	Level = InLevel;
	OnLevelChangedDelegate.Broadcast(Level);
}

void AAuraPlayerState::AddToXP(int32 InXP)
{
	XP += InXP;
	OnXPChangedDelegate.Broadcast(XP);
}

void AAuraPlayerState::SetXP(int32 InXP)
{
	XP = InXP;
	OnXPChangedDelegate.Broadcast(XP);
}

void AAuraPlayerState::AddToAttributePoints(int32 InAttributePoints)
{
	AttributePoints += InAttributePoints;
	OnAttributePointsChangedDelegate.Broadcast(AttributePoints);
}

void AAuraPlayerState::SetAttributePoints(int32 InAttributePoints)
{
	AttributePoints = InAttributePoints;
	OnAttributePointsChangedDelegate.Broadcast(AttributePoints);
}

void AAuraPlayerState::AddToSpellPoints(int32 InSpellPoints)
{
	SpellPoints += InSpellPoints;
	OnSpellPointsChangedDelegate.Broadcast(SpellPoints);
}

void AAuraPlayerState::SetSpellPoints(int32 InSpellPoints)
{
	SpellPoints = InSpellPoints;
	OnSpellPointsChangedDelegate.Broadcast(SpellPoints);
}

void AAuraPlayerState::AddToMoney(int32 InMoney)
{
	//InMoney若为负数，则为消耗经济
	Money += InMoney;
	OnMoneyChangedDelegate.Broadcast(Money);
}

void AAuraPlayerState::SetMoney(int32 InMoney)
{
	Money = InMoney;
	OnMoneyChangedDelegate.Broadcast(Money);
}

void AAuraPlayerState::OnReq_Level(int32 OldLevel)
{
	OnLevelChangedDelegate.Broadcast(Level); //上面修改委托只会在服务器触发，在此处设置是在服务器更新到客户端本地后触发
}

void AAuraPlayerState::OnReq_XP(int32 OldXP)
{
	OnXPChangedDelegate.Broadcast(XP); //上面修改委托只会在服务器触发，在此处设置是在服务器更新到客户端本地后触发
}

void AAuraPlayerState::OnReq_Money(int32 OldMoney)
{
	OnMoneyChangedDelegate.Broadcast(Money);
}

void AAuraPlayerState::OnRep_AttributePoints(int32 OldAttributePoints)
{
	OnAttributePointsChangedDelegate.Broadcast(AttributePoints);
}

void AAuraPlayerState::OnReq_SpellPoints(int32 OldSpellPoints)
{
	OnSpellPointsChangedDelegate.Broadcast(SpellPoints);
}